<template>
  <div class="wallet-detail">
    <!-- title 通用组件中 是否显示标题 -->
    <!-- titleText 显示标题的内容 -->
    <wallet-title :title="queryName">
      <template #right v-if="rightFlag">

      </template>
    </wallet-title>
    <div v-if="queryIndex === 0">
      <l-text-rarrow
        v-for="(item, index) in walletDetailList"
        :key="index"
        :icon="item.svg"
        :text="item.text"
        :distance="item.description"
        :icon2="item.arrow"
        @click.native="getDepositDialog"
        :class="item.class"
      />
        <!-- @click.native="item.click !== '' ? depositFlag = true : depositFlag = true" -->
      <van-dialog v-model="depositFlag" confirm-button-text="确认充值" @confirm="getRechargeMoney">
        <van-nav-bar
          left-arrow
          @click-left="depositFlag = false"
        >
          <template #right>
            <slot name="right"></slot>
          </template>
          <slot name="searchSlot"></slot>
        </van-nav-bar>
        <h4 style="text-align: center;">余额充值</h4>
        <div class="detail2-text">
          <div class="text2-left" ref="textLeft">
            <span>充值方式</span>
          </div>
          <div class="text2-right">
            <span>微信支付</span>
            <van-icon name="arrow"></van-icon>
          </div>
        </div>
        <van-cell-group>
          <van-field v-model="recharge" label="充值金额" placeholder="请输入金额数" />
        </van-cell-group>
      </van-dialog>
    </div>
    <div v-if="queryIndex === 1">
      <div class="myIntegral">
        <div class="integral-title">
          <span>我的积分</span>
          <van-icon name="question-o" />
        </div>
        <div class="integral-detail">
          <div class="integral-score">
            <div>0</div>
            <div>明细&gt;</div>
          </div>
          <div class="integral-sign">
            <van-icon name="sign" />
            <span>签到</span>
          </div>
        </div>
      </div>
      <div class="integral-coupon">
        <div class="integral-coupon-box" v-for="(icBoxItem, icBoxIndex) in icBoxList" :key="icBoxIndex">
          <div class="integral-coupon-img">
            <img :src="icBoxItem.imgSrc" alt="">
          </div>
          <div class="integral-coupon-div">
            <h4>{{icBoxItem.title}}</h4>
            <p>{{icBoxItem.p}}</p>
            <div>
              <span>{{icBoxItem.btn}}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div v-if="queryIndex === 2">
      <div class="coupon" v-for="(couponItem, couponIndex) in couponList" :key="couponIndex">
        <div class="coupon-left">
          <span>{{couponItem.price}}</span>
        </div>
        <div class="coupon-right">
          <h3>{{couponItem.require}}</h3>
          <ol type="1">
            <li v-for="(couponOlItem, couponOlIndex) in couponItem.list" :key="couponOlIndex">{{couponOlItem.cont}}</li>
          </ol>
        </div>
      </div>
    </div>
    <div v-if="queryIndex === 3">
      <function-feedback />
    </div>
  </div>
</template>

<script>
import { getDeposit, updateDeposit } from "../../../api";
import LTextRarrow from "../../common/LTextRarrow.vue";
import WalletTitle from "../../common/WalletTitle.vue";
import FunctionFeedback from "./FunctionFeedback.vue";
export default {
  name: "walletDetail",
  components: { WalletTitle, LTextRarrow, FunctionFeedback },
  data() {
    return {
      queryName: '',
      depositDialog: '',
      recharge: '', // 充值的金额
      queryIndex: '',
      defaultTitle: '兑换记录',
      rightFlag: true,
      walletDetailList: [
        {
          svg: "#icon-zhanghu",
          text: "账户余额",
          description: "0",
          arrow: "arrow",
          class: 'zhanghu',
          click: 'getDepositDialog'
        },
        {
          svg: "#icon-youhuiquan",
          text: "优惠券",
          description: "0",
          arrow: "arrow",
          class: 'youhui',
          click: 'getYouhui'
        },
        {
          svg: "#icon-jinbi",
          text: "抵用金",
          description: "0",
          arrow: "arrow",
          class: 'diyong',
          click: 'getDiyong'
        },
        {
          svg: "#icon-jinbi1",
          text: "悟空币",
          description: "0",
          arrow: "arrow",
          class: 'bi',
          click: 'getBi'
        },
        {
          svg: "#icon-ticket_integral",
          text: "积分",
          description: "0",
          arrow: "arrow",
          class: 'jifen',
          click: 'getJifen'
        }
      ],
      couponList: [
        {
          price: 1950,
          require: '满1000减10',
          list: [
            {cont: '满1000减10，满1天才可用，仅超值月租订单可用'},
            {cont: '有效期：2022.03.14-2022.05.14'}
          ]
        },
        {
          price: 1950,
          require: '满1000减10',
          list: [
            {cont: '满1000减10，满1天才可用，仅超值月租订单可用'},
            {cont: '有效期：2022.03.14-2022.05.14'}
          ]
        },
        {
          price: 1950,
          require: '满1000减10',
          list: [
            {cont: '满1000减10，满1天才可用，仅超值月租订单可用'},
            {cont: '有效期：2022.03.14-2022.05.14'}
          ]
        },
        {
          price: 1950,
          require: '满1000减10',
          list: [
            {cont: '满1000减10，满1天才可用，仅超值月租订单可用'},
            {cont: '有效期：2022.03.14-2022.05.14'}
          ]
        },
        {
          price: 1950,
          require: '满1000减10',
          list: [
            {cont: '满1000减10，满1天才可用，仅超值月租订单可用'},
            {cont: '有效期：2022.03.14-2022.05.14'}
          ]
        },
        {
          price: 1950,
          require: '满1000减10',
          list: [
            {cont: '满1000减10，满1天才可用，仅超值月租订单可用'},
            {cont: '有效期：2022.03.14-2022.05.14'}
          ]
        }
      ],
      icBoxList: [
        {
          imgSrc: 'https://img.yzcdn.cn/vant/cat.jpeg',
          title: '50元租车券',
          p: '2000积分',
          btn: '兑换'
        },
        {
          imgSrc: 'https://img.yzcdn.cn/vant/cat.jpeg',
          title: '50元租车券',
          p: '2000积分',
          btn: '兑换'
        },
        {
          imgSrc: 'https://img.yzcdn.cn/vant/cat.jpeg',
          title: '50元租车券',
          p: '2000积分',
          btn: '兑换'
        },
        {
          imgSrc: 'https://img.yzcdn.cn/vant/cat.jpeg',
          title: '50元租车券',
          p: '2000积分',
          btn: '兑换'
        },
        {
          imgSrc: 'https://img.yzcdn.cn/vant/cat.jpeg',
          title: '50元租车券',
          p: '2000积分',
          btn: '兑换'
        }
      ],
      deposit: '',
      depositFlag: false // 充值界面状态
    };
  },
  mounted() {
    this.getParameter();
    this.getUserDeposit();
  },
  methods: {
    // 拿到充值的金额
    getRechargeMoney() {
      this.depositFlag = true;
      this.walletDetailList[0].description = parseFloat(this.walletDetailList[0].description) + parseFloat(this.recharge) + "";
      updateDeposit({ // 将充值后的钱加上余额更新到数据库中
        "deposit": this.walletDetailList[0].description,
        "id": this.$cookie.get('userId')
      }).then(() => {
      })
    },
    getYouhui() {},
    getDiyong() {},
    getBi() {},
    getJifen() {},
    // 让充值界面弹框显示
    getDepositDialog() {
      this.depositFlag = true;
    },
    // 获取钱包余额
    getUserDeposit() {
      getDeposit({
        "id": this.$cookie.get('userId')
      }).then(({data}) => {
        // 将前台的钱包余额跟数据库同步
        this.walletDetailList[0].description = (data.data[0].deposit + "") !== "null" ? (data.data[0].deposit + "") : '0';
        if(this.walletDetailList[0].description === "0") {
          console.log("没钱了，需要充值")
          this.$dialog.confirm({
            message: '需要进行余额充值吗？'
          }).then(() => {
            // 显示充值余额界面
            this.depositFlag = true;
          }).catch(() => {
            return false;
          })
        }
      })
    },
    getParameter() {
      this.queryName = this.$route.query.title;
      this.queryIndex = parseInt(this.$route.query.idx);// 将字符串转换为数值

      if(this.queryIndex === 3) {
        process.nextTick(() => {
          this.$refs.credit.style.height =
            document.documentElement.clientHeight + "px";
        })
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.wallet-detail /deep/ {
  width: 100%;
  height: 100%;
  background-color: #fff;
  .wallet-title {
    border-bottom: 4px solid $bgcolor;
  }
  // 钱包
  .detail-text {
    border: 4px solid #fff;
    .text-left {
      svg {
        color: #222;
      }
    }
  }
  // 积分
  .myIntegral {
    width: 100%;
    height: $height*3;
    padding-left: 15px;
    box-sizing: border-box;
    .integral-title {
      display: flex;
      justify-content: flex-start;
      padding-top: 10px;
      box-sizing: border-box;
      .van-icon {
        margin-top: 3px;
      }
    }
    .integral-detail {
      width: 100%;
      height: $height*2;
      line-height: $height*2;
      display: flex;
      justify-content: space-between;
      padding-right: 10px;
      box-sizing: border-box;
      .integral-score {
        display: flex;
        div {
          &:nth-of-type(1) {
            font-size: 20px;
            font-weight: 700;
            &::after {
              content: '分';
              font-size: 14px;
              font-weight: 400;
            }
          }
          &:nth-of-type(2) {
            margin-left: 10px;
            font-size: 14px;
          }
        }
      }
      .integral-sign {
        width: $height*2;
        height: $height;
        line-height: $height;
        text-align: center;
        background-color: $color;
        color: #fff;
        border-radius: 10px;
        position: relative;
        .van-icon {
          position: absolute;
          top: 50%;
          left: 15px;
          transform: translateY(-50%);
        }
        span {
          font-size: 14px;
          margin-left: 20px;
        }
      }
    }
  }
  .integral-coupon {
    display: flex;
    justify-content: flex-start;
    flex-flow: row wrap;
    .integral-coupon-box {
      width: 46%;
      margin-left: 2.7%;
      box-shadow: 0 0 6px rgb(200, 200, 200);
      margin-bottom: 10px;
      border-radius: 4px;
      .integral-coupon-img {
        width: 100%;
        height: $height*3;
        img {
          width: 100%;
          height: 100%;
        }
      }
      .integral-coupon-div {
        width: 100%;
        height: $height*2;
        padding-left: 10px;
        box-sizing: border-box;
        h4 {
          font-size: 16px;
          margin-top: 10px;
        }
        p {
          font-size: 14px;
          color: red;
        }
        div {
          display: flex;
          justify-content: flex-end;
          span {
            margin-right: 10px;
            background-color: red;
            color: #fff;
            text-align: center;
            width: 40px;
            height: $height/1.5;
            line-height: $height/1.5;
            font-size: 12px;
            border-radius: 4px;
          }
        }
      }
    }
  }
  // 优惠券
  .coupon {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: space-between;
    border-bottom: 4px solid $bgcolor;
    .coupon-left {
      width: 30%;
      height: $height*3;
      line-height: $height*3;
      text-align: center;
      span {
        font-size: 20px;
        font-weight: 700;
        color: $color;
        &::before {
          content: '￥';
          font-size: 12px;
        }
      }
    }
    .coupon-right {
      width: 70%;
      height: $height*3;
      padding-top: 20px;
      box-sizing: border-box;
      h3 {
        font-size: 14px;
      }
      ol {
        li {
          font-size: 12px;
          color: #666;
        }
      }
    }
  }

  // 信用卡
  .credit-card {
    width: 100%;
    height: auto;
    text-align: center;
    background-color: #fff;
    font-size: 14px;
    p {
      color: #666;
      margin-top: $height*7;
    }
    div {
      width: 80%;
      height: $height;
      line-height: $height;
      color: #fff;
      background-color: $color;
      border-radius: 4px;
      margin: 0 auto;
      margin-top: $height;
    }
  }
}
.detail2-text {
  width: 100%;
  height: $height*1.5;
  line-height: $height*1.5;
  display: flex;
  justify-content: space-between;
  padding-left: 15px;
  box-sizing: border-box;
  .text2-left {
    width: auto;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    .svg-icon {
      color: $color;
    }
  }
  .text2-right {
    padding-right: 20px;
    box-sizing: border-box;
    font-size: 12px;
    display: flex;
    align-items: center;
  }
}
.youhui, .diyong, .bi, .jifen {
  pointer-events: none;
}
</style>